package com.example.himalayaproject.play.noncontent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.play.noncontent.bean.PlayDate;
import com.example.himalayaproject.subscription.view.CircleTransformation;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/12.
 */
public class FragmentPlayAdapter extends RecyclerView.Adapter<FragmentPlayAdapter.ViewHolder> {
    private List<PlayDate> list;
    private Context context;
    private onNextPageListener listener;
    private int page = 0;

    public FragmentPlayAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setListener(onNextPageListener listener) {
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.fragment_play_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(list.get(position).getTitle());
        holder.nickName.setText(list.get(position).getNickname());
         holder.time.setText(list.get(position).getUpdatedAt());
         holder.month.setText(list.get(position).getCreatedAt());
        DecimalFormat  decimalFormat = new DecimalFormat(".0");
        double number = Integer.parseInt(list.get(position).getPlaysCounts()) / 10000;
        if (number > 9999) {
            number = number / 10000;
            holder.lookNumber.setText(decimalFormat.format(number) + "亿");
        } else {
            holder.lookNumber.setText(decimalFormat.format(number) + "万");
        }
        holder.comment.setText(list.get(position).getCommentsCounts());
        holder.month.setText(list.get(position).getCreatedAt());
        Picasso.with(context).load(list.get(position).getCoverSmall()).transform(new CircleTransformation()).into(holder.imageView);
       if(position == list.size()-1){
           page = page+1;
           listener.onNextPage(page);
       }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(Collection< ? extends PlayDate> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size,collection.size());
    }



    public interface  onNextPageListener{
        void  onNextPage(int page);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView title, lookNumber, time, month, comment, nickName;
        private LinearLayout linearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.fragment_play_item_image);
            title = (TextView) itemView.findViewById(R.id.fragment_play_item_title1);
            lookNumber = (TextView) itemView.findViewById(R.id.fragment_play_item_looker_number);
            time = (TextView) itemView.findViewById(R.id.fragment_play_item_time_tv);
            month = (TextView) itemView.findViewById(R.id.fragment_play_item_month_tv);
            comment = (TextView) itemView.findViewById(R.id.fragment_play_item_comment_tv);
            nickName = (TextView) itemView.findViewById(R.id.fragment_play_item_nickName);
        }
    }
}
